package com.lagou.service.code.controller;

import com.lagou.dubbo.service.EmailService;
import com.lagou.dubbo.service.CodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

//    @Reference(check = false, version = "${dubbo.service.version}")
    @Reference
    private EmailService emailService;

    /**
     * ⽣成验证码并发送到对应邮箱，成功true，失败false
     */
    @PostMapping("/create/{email}")
    public Boolean create(@PathVariable @NotBlank(message = "邮箱不能为空") String email) {

        /** 1 生成验证码 */
        String code = codeService.create(email);

        /** 2 发送验证码（调用微服务接口） */
        return emailService.send(email, code);
    }

    /**
     * 校验验证码是否正确，0正确1错误2超时
     */
    @GetMapping("/validate/{email}/{code}")
    public Integer validate(@PathVariable @NotBlank(message = "邮箱不能为空") String email,
                            @PathVariable @NotBlank(message = "验证码不能为空") String code) {
        return codeService.validate(email, code);
    }
}
